package com.xlx.openLink.controller;

import com.xlx.openLink.base.BaseDateMessage;
import com.xlx.openLink.base.ThreadRunTimeExcption;
import com.xlx.openLink.entity.pojo.Port;
import com.xlx.openLink.enums.ResultCodeEnum;
import com.xlx.openLink.service.PortService;
import com.xlx.openLink.service.SpeedService;
import com.xlx.openLink.vo.RequestPortLst;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 端口管路
 */
@Slf4j
@Controller
@RequestMapping("/port")
public class PortController {


    @Autowired
    private PortService portService;

    @Autowired
    private SpeedService speedService;

    /**
     * 分页操作
     *
     * @param requestPortLst
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @ApiImplicitParam(name = "requestPortLst", value = "分页请求参数", required = true, dataType = "requestPortLst")
    @PostMapping("/listByPage")
    @ResponseBody
    public BaseDateMessage listByPage(@RequestBody RequestPortLst requestPortLst) {
        BaseDateMessage baseDateMessage = new BaseDateMessage();
        try {
            baseDateMessage.setData(portService.listByPage(requestPortLst));
            baseDateMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseDateMessage.setMsg("获取成功");
        } catch (ThreadRunTimeExcption excption) {
            baseDateMessage.buildSuccess(excption.getResultCodeEnum());
        } catch (Exception e) {
            log.info(e.getMessage());
            baseDateMessage.buildFail(ResultCodeEnum.FAIL);
            baseDateMessage.setMsg("获取失败");
        }
        return baseDateMessage;
    }


    @ResponseBody
    @PostMapping("/update")
    public BaseDateMessage update(@RequestBody Port port) {
        BaseDateMessage baseDateMessage = new BaseDateMessage();
        try {
            baseDateMessage.setData(portService.update(port));
            baseDateMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseDateMessage.setMsg("更新成功");
        } catch (ThreadRunTimeExcption excption) {
            baseDateMessage.buildSuccess(excption.getResultCodeEnum());
        } catch (Exception e) {
            log.info(e.getMessage());
            baseDateMessage.buildFail(ResultCodeEnum.FAIL);
            baseDateMessage.setMsg("更新失败");
        }
        return baseDateMessage;
    }

    @ResponseBody
    @GetMapping("/selectById")
    public BaseDateMessage selectById(@RequestParam(value = "pkId") String pkId) {
        BaseDateMessage baseDateMessage = new BaseDateMessage();
        try {
            baseDateMessage.setData(portService.selectById(pkId));
            baseDateMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseDateMessage.setMsg("获取成功");
        } catch (ThreadRunTimeExcption excption) {
            baseDateMessage.buildSuccess(excption.getResultCodeEnum());
        } catch (Exception e) {
            log.info(e.getMessage());
            baseDateMessage.buildFail(ResultCodeEnum.FAIL);
            baseDateMessage.setMsg("获取成功");
        }
        return baseDateMessage;
    }


    @ResponseBody
    @GetMapping("/selectListList")
    public BaseDateMessage selectListList(@RequestParam(value = "pkId") String pkId) {
        BaseDateMessage baseDateMessage = new BaseDateMessage();
        try {
            baseDateMessage.setData(speedService.selectListList(pkId));
            baseDateMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseDateMessage.setMsg("获取成功");
        } catch (ThreadRunTimeExcption excption) {
            baseDateMessage.buildSuccess(excption.getResultCodeEnum());
        } catch (Exception e) {
            log.info(e.getMessage());
            baseDateMessage.buildFail(ResultCodeEnum.FAIL);
            baseDateMessage.setMsg("获取失败");
        }
        return baseDateMessage;
    }


}
